package com.it.travel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.travel.dao.StoryMapper;
import com.it.travel.pojo.Story;
import com.it.travel.service.StoryService;
import com.it.travel.vo.Result;
import com.it.travel.vo.StoryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StoryServiceImpl implements StoryService {

    @Resource
    private StoryMapper storyMapper;


    @Override
    public Result<?> getStoryInfo(Integer num) {
        LambdaQueryWrapper<Story> wrapper = Wrappers.lambdaQuery();
        if (num == 0) {
            wrapper.orderByDesc(Story::getCreateTime);
        } else {
            wrapper.orderByAsc(Story::getCreateTime);
        }
        List<Story> storyList = storyMapper.selectList(wrapper);
        List<StoryVO> list = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Story story : storyList) {
            StoryVO storyVO = new StoryVO();
            BeanUtil.copyProperties(story, storyVO);
            String format = simpleDateFormat.format(new Date(story.getCreateTime()));
            storyVO.setFotmatTime(format);
            list.add(storyVO);

        }
        return Result.success(list, "yes");
    }

    @Override
    public Result<?> getStoryDetail(Integer id) {
        Story story = storyMapper.selectById(id);
        return Result.success(story, "yes");

    }
}
